


function validate(e)
{        
                
    //get the value of the element paper          
    text = document.getElementById("paper").value;
    //get the last index of the "\"
    var indexOfSlash = text.lastIndexOf('\\');
    //get the file name which is the after the last "\" and set it to a variable
    var file = text.substring(indexOfSlash + 1);
               
    //check if there is any illegal character in the file name using regular expressions then print a message for the user
    if(file.match(/^[\w\.]+$/) == null) {
        alert ("Warning! Illegal filename!");
        return false;
    }
    return true;
}

// Email name counter
var x = 0;
//function to add text feild
function add(type) {

    //Create an input type dynamically.
    var element = document.createElement("input");

    //Assign different attributes to the element.
    type =  type + x;
    element.setAttribute("type", 'email');
    element.setAttribute("name", type);
    //Create a fooBar type dynamically.
    var foo = document.getElementById("fooBar");

    //Append the element in page (in span).
    foo.appendChild(element);
                
    x = x + 1;
}
        
//Function to accept letters only
function lettersOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
        ((evt.which) ? evt.which : 0));
    //Seting all characters that are non-acceptable in the textbox
    if (charCode > 31 && (charCode < 65 || charCode > 90) &&
        (charCode < 97 || charCode > 122)) {
        // alert if any was entered by the user
        alert("Please enter letters only.");
        return false;
    }
    return true;
}
        
//Function to numbers letters only
function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    //Setting the acceptable numbers that could be entered in the textbox
    if (charCode != 46 && charCode > 31 
        && (charCode < 48 || charCode > 57)){
        // alert if any unacceptable character was entered by the user
        alert("Please enter numbers only.");
            
        return false;
    }
        

    return true;
}

function valid(){
    if(document.getElementById('username').value.toString().length>=1 || document.getElementById('password').value.toString().length>=1){
        return true;
    }
    else{
        alert("The Combination of username and password are Incorrect !");
        return false;
    }
}

function timeMsg() //called automatically when the page is opened
{
    setTimeout("alertMsg()",1800000); //1800000 = 30 min
}
function alertMsg()
{
    document.getElementById("toDisable").disabled = true;
    alert('Please re-open the page from the link provided in the mail');
}

            
$(document).ready(function() {
    $('#warning a').click(function(e) {
        e.preventDefault();
        var page = $(this).attr('href');
        $('#aboutus').fadeOut('slow',function() {
            $('#aboutus').load(page, function () {
                $("#aboutus").fadeIn('slow');
                return false;
            });
        });

    });
    $('#warning3 a').click(function(e) {
        e.preventDefault();
        var page = $(this).attr('href');
        $('#privacypolicy').fadeOut('slow',function() {
            $('#privacypolicy').load(page, function () {
                $("#privacypolicy").fadeIn('slow');
                return false;
            });
        });
        return false;
    });
    $('#warning2 a').click(function(e) {
        e.preventDefault();
        var page = $(this).attr('href');
        $('#termsofservice').fadeOut('slow',function() {
            $('#termsofservice').load(page, function () {
                $("#termsofservice").fadeIn('slow');
                return false;
            });
        });
        return false;
    });
    $('#ffo #t').click(function(e) {
        e.preventDefault();
        $("#cover2").fadeIn('slow');
        var page = $(this).attr('href');
        $('#termsofservice').fadeOut('slow',function() {
            $('#termsofservice').load(page, function () {
                $("#termsofservice").fadeIn('slow');
                return false;
            });
        });
        return false;
    });
    $('#ffo #p').click(function(e) {
        e.preventDefault();
        $("#cover3").fadeIn('slow');
        var page = $(this).attr('href');
        $('#privacypolicy').fadeOut('slow',function() {
            $('#privacypolicy').load(page, function () {
                $("#privacypolicy").fadeIn('slow');
                return false;
            });
        });
        return false;
    });
    $('#ffo #a').click(function(e) {
        e.preventDefault();
        $("#cover").fadeIn('slow');
        var page = $(this).attr('href');
        $('#aboutus').fadeOut('slow',function() {
            $('#aboutus').load(page, function () {
                $("#aboutus").fadeIn('slow');
                return false;
            });
        });
        return false;
    });
    $('#cover').click(function() {
        $("#cover").fadeOut('slow');
    });
    $('#cover2').click(function() {
        $("#cover2").fadeOut('slow');
    });
    $('#cover3').click(function() {
        $("#cover3").fadeOut('slow');
    });
});
     

//a function that allows only numbers and letters to be entered
function alpha(e) {
    var k;
    document.all ? k = e.keyCode : k = e.which;
    return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57));
}
                    
//a function that allows only numbers to be entered                
function isNumberOnly(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)){
        alert("Please enter numbers only.");
        return false;
    }
    return true;
}
//a function that allows only letters to be entered
function isLetterOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 65 || charCode > 90) &&
        (charCode < 97 || charCode > 122)) {
        alert("Please enter letters only.");
        return false;
    }
    return true;
}

//a function that takes the action and changes the status of the submit button from
//disabled to enabled or vice versa
function change(e)
{
    document.form.send_button.disabled=true;
    if(e.checked==true)
    {
        document.form.send_button.disabled=false;
    }
    if(e.checked==false)
    {
        document.form.send_button.enabled=false;
    }
}

//a function isAlphabet is created taking as a parameter the element
//doing the action
function isAlphabetNum(elem){
    //a variable is initialized that stores a regular expression for letters, numbers and underscores.
    var alphaExp = /^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$/;
    //The condition checks that the value of the element matches the reg-ex
    if(elem.value.match(alphaExp)){
        // in the case it does, the function returns true.
        return true;
    } else{
        //if it doesnt, an alert message is displayed and the 
        //function returns false.
        alert("please enter text only");
        return false;
    }
                
}

//this part of the code is for hiding and showing the tables of authors and reviewers on clicking them
$(document).ready(function(){
    $(".flip1").click(function(){
        $(".panel1").slideToggle("slow");
    });
    $(".flip2").click(function(){
        $(".panel2").slideToggle("slow");
    });
});
            


//activates the text box on selecting the thank you template and changes the text inside according to the template
function activateThankYou()
{
    document.forms[0].TextBox.disabled=false;
    document.forms[0].TextBox.value="thank you for the reviews";

    document.forms[0].Message.disabled=false;

    for(var i=0;i<document.forms[0].elements.length;i++)
    {
        if(document.forms[0].elements[i].name=="dOption")
        {
            if(document.forms[0].elements[i].value=="N")
            {
                if(document.forms[0].elements[i].checked==true)
                {
                    document.forms[0].TextBox.disabled=false;
                    document.forms[0].TextBox="thank you for the reviews";
                    document.forms[0].Message.disabled=false;
                }
            }
        }
    }
}
//activates the text box on selecting the reminder template and changes the text inside according to the template
function activateReminder()
{
    // initialize form with empty field

    document.forms[0].TextBox.disabled=false;
    document.forms[0].TextBox.value="we would like to remind you that the deadline for ... is on the ...";

    document.forms[0].Message.disabled=false;

    for(var i=0;i<document.forms[0].elements.length;i++)
    {
        if(document.forms[0].elements[i].name=="dOption")
        {
            if(document.forms[0].elements[i].value=="T")
            {
                if(document.forms[0].elements[i].checked==true)
                {
                    document.forms[0].TextBox.disabled=false;
                    document.forms[0].TextBox.value="we would like to remind you that the deadline for ... is on the ...";
                    document.forms[0].Message.disabled=false;
                }
            }
        }
    }
}
//activates the text box on selecting the empty template and changes the text inside according to the template
function activateEmpty()
{
    document.forms[0].TextBox.disabled=false;
    document.forms[0].TextBox.value=".....";

    document.forms[0].Message.disabled=false;

    for(var i=0;i<document.forms[0].elements.length;i++)
    {
        if(document.forms[0].elements[i].name=="dOption")
        {
            if(document.forms[0].elements[i].value=="T")
            {
                if(document.forms[0].elements[i].checked==true)
                {
                    document.forms[0].TextBox.disabled=false;
                    document.forms[0].TextBox.value="....";
                    document.forms[0].Message.disabled=false;
                }
            }
        }
    }
}
//creating the radio button list and the text box
        
function validateDate(date)
{
    now= new Date(); // this now variable  takes a date variable and it is supposed to get the local date
    regexpDate=/^\d{4}(-|\/)([1-9]|0[1-9]|1[0-2])(-|\/)([1-9]|0[1-9]|[1-2][0-9]|3[0-1])$/; // i am making a regular expression that checks on the following :  first this sign ^  means that i am looking from the beginning of  string , d{4} means that i am making 4 characters that are numbers in order for the user to be able to enter the year then it is followed by a - or / which are allowed  then it should be followed by months so i wrote that if he enters a month starting from 0 to 9  or the first starting character is 0 followed  then by numbers from 0 to 9 or  the first character is 1 followed by numbers from 0 to 2(this checks that the users chooses a right month number) , then i am making sure again that there is a - or /  that is followed  by the right date number  then i entered the $ sign which means the end of the text area  and this was to make sure that this regular expression is read from the start till the end of the text area
    if(date.match(regexpDate)==null) // i am making sure that the date is not null
        return false;
    else
    {
        /* this arr function will split at each - or / that he finds
                    this new variable is  take an array of dates (which means that arr[0] takes the year that the user has entered followed by arr[1]-1 that takes the monthes but since the array takes it from 0 till 11 i used the -1, arr[3] takes the date
                    checking that input dates are  not older than the local date */
        arr=date.split(/[-|\/]+/);
        input=new Date(arr[0],arr[1]-1,arr[2]);
        if(input<now)
            return false;
        else
            return true;
    }
}

function init(ID) {
    //add date picker to each date field
    calendar.set(ID);
}
            
// wait for the document to be ready before starting the script
$(document).ready(function(){
    $("#shortpapercheckbox").bind("click",function()
    {
        if($(this).is(":checked"))
            $("#shortpapersubmissiondate").removeAttr("disabled");
        else
            $("#shortpapersubmissiondate").attr("disabled","disabled");
    });
    $("#longpapercheckbox").bind("click",function()
    {
        if($(this).is(":checked"))
            $("#longpapersubmissiondate").removeAttr("disabled");
        else
            $("#longpapersubmissiondate").attr("disabled","disabled");
    });

    $("#Image").bind("click",function()
    {
        if($(this).is(":checked"))
            $("#imagesubmissiondate").removeAttr("disabled");
        else
            $("#imagesubmissiondate").attr("disabled","disabled");
    });

    $("#Video").bind("click",function()
    {
        if($(this).is(":checked"))
            $("#videosubmissiondate").removeAttr("disabled");
        else
            $("#videosubmissiondate").attr("disabled","disabled");
    });

    $("#Poster").bind("click",function()
    {
        if($(this).is(":checked"))
            $("#postersubmissiondate").removeAttr("disabled");
        else
            $("#postersubmissiondate").attr("disabled","disabled");
    });
    $("#other").bind("click",function()
    {
        if($(this).is(":checked"))
            $("#othersubmissiondate,#custom_type").removeAttr("disabled");
        else
            $("#othersubmissiondate,#custom_type").attr("disabled","disabled");
    });

    $("#custom_committee").bind("click",function()
    {
        if($(this).is(":checked"))
            $("#custom_text").removeAttr("disabled");
        else
            $("#custom_text").attr("disabled","disabled");
    });
    $("#Submitbutton").bind('click',function(){

        // stores the string from the text area and stores it in a new variable
        longname = $("#LongName").val();
        shortname = $("#Shortname").val();
        textareadescription = $("#description").val();
        //check if the checkbox is checked , and returns either true (in case checkbox checked  )else it returns false
        spaper = $("#shortpapercheckbox").is(':checked');
        lpaper = $("#longpapercheckbox").is(':checked');
        spapersub = $("#shortpapersubmissiondate").val();
        lpapersub=$("#longpapersubmissiondate").val();
        isub=$("#imagesubmissiondate").val();
        vsub=$("#videosubmissiondate").val();
        psub=$("#postersubmissiondate").val();
        //check if the checkbox is checked , it returns either true or false
        com1 =  $("#committe1checkbox").is(':checked');
        com2 = $("#committe2checkbox").is(':checked');
        com3 = $("#committe3checkbox").is(':checked');
        custom_com=$("#custom_committee").is(":checked");
        customcomtext=$("#custom_text").val();
        papertype =$("#papertypes").val();
        conference =$("#previousconf").val();
        image=$("#Image").is(":checked");
        video=$("#Video").is(":checked");
        poster=$("#Poster").is(":checked");
        other=$("#other").is(":checked");
        custom_type=$("#custom_type").val();
        place=$("#place").val();
        date=$("#confdate").val();
        if (longname != "" && shortname != "" && place != "")
        {
            if(spaper == true | lpaper == true | image == true | video == true | poster == true | other == true)
            {
                if (com1 == true | com2 == true | com3 == true | other == true)
                {

                    $.post("conferenceSetupServerSide.php",{
                        longname: longname , 
                        shortname: shortname ,
                        place:place,
                        date:date,
                        textarea: textareadescription ,
                        image:image,
                        video:video,
                        poster:poster, 
                        Spaper: spaper , 
                        Lpaper: lpaper , 
                        spapersub: spapersub,
                        lpapersub:lpapersub,
                        isub:isub,
                        vsub:vsub,
                        psub:psub ,
                        comm1: com1 , 
                        comm2: com2 , 
                        comm3: com3 ,
                        other:other,
                        custom_type:custom_type,
                        custom_com: custom_com,
                        custom_com_text:customcomtext, 
                        papertype: papertype , 
                        conference: conference
                    },function(data)  //sending all the page values to the server while storing them in new variables
                    {
                        // this checks that if there are any errors that were found then it should be alerted and the function stops. Else there are no errors in the data will be alerted then it will direct the user to the main menu.
                        if(data.indexOf("Error")==0)
                        {
                            alert(data);
                        }
                        else
                        {
                            // if no error ocuured redirect to the main page
                            alert(data);
                            top.window.location= 'main.php';

                        }
                    })
                }
                else
                {
                    alert('please make sure that you chose a committee')
                }
            }
            else
            {
                alert (' please make sure that you have chosen a submission type');
            }
        }
        else {
            alert(' place make sure that you entered the long conference name or the short conference name or place successfully');
        }
    });

});


$(document).ready(function(){
    $("#signin").click(function(){
        $("#cover5").fadeIn('slow');
    });
    $("#cover5").click(function(){
        $("#cover5").fadeOut('slow');
    });
    $("#start").click(function(){
        $("#cover4").fadeIn('slow');
    });
    $("#cover4").click(function(){
        $("#cover4").fadeOut('slow');
    });
    $("#signup").click(function(){
        $("#cover6").fadeIn('slow');
    });
    $("#cover6").click(function(){
        $("#cover6").fadeOut('slow');
    });
});
            

$(document).ready(function(){
    $('#collabsoft').load (function() { // function to make the iframe auto resize it's height according to the height of the content
        $(this).height($(this).contents().find("html").height()+400);

    });
});
            
$(document).ready(function () {
    $('#nav a').click(function () {
        $('#nav a').removeClass('selected');
        $(this).addClass('selected');
    });
    if ($("#nav").is(':visible')) {

        $("#collabsoft").attr("width", "80%"); // setting the width of the iframe if the menu is visible or not
    }
    else {
        $("#collabsoft").attr("width", "100%");
    }
    $vis = 1;
    $(".flip").click(function () { // click function to show hide the menu and animate the iframe to fit the whole width of the page
        if ($vis) {
            $vis = 0;
            $("#nav").animate({
                left:'-=250px'
            }, 'slow');
            jQuery("#collabsoft").animate({
                width: '100%'
            }, 'slow');
        }
        else {
            $vis = 1;
            $("#nav").animate({
                left:'+=250px'
            }, 'slow');
            jQuery("#collabsoft").animate({
                width: '80%'
            }, 'slow');
        }
    });
    return false;
});


//change the taxt in the label to view errors or success massages
function success(text) {
    document.getElementById("label2").style.color = "green"
    document.getElementById("label2").innerHTML= text;
}
function error(text) {
    document.getElementById("label2").style.color = "red"
    document.getElementById("label2").innerHTML= text;
}
function ChangeTextCrit(text) {
    document.getElementById("label3").innerHTML= text;
}
//Author: Aya El Mobayad
//a javascript function is created taking as a parameter the checked status
//of the checkbox.
function viewCustomFields(status){
    //If a status is true, It is assigned its inverse value which is false.
    status=!status;
    //The textboxes on the page are called and their disabled attribute is set
    //to the boolean value of the status variable. If the status is false,the boxes
    //are disabled, if true, they are enabled.
    document.form.Cname.disabled = status;
    document.form.Cdesc.disabled = status;
    //If the boxes are disabled, the span color will remain grey
    if (status == true){
        document.getElementById('cust').style.color = 'grey';
    }
    //if the boxes are enabled, the span color will change to black.
    else {
        document.getElementById('cust').style.color = 'black';
    }
                
}
//a function isAlphabet is created taking as a parameter the element
//doing the action
function isAlphabet(elem){
    //a variable is initialized that stores a regular expression for letters and underscores.
    var alphaExp = /^[A-Za-z _]*[A-Za-z][A-Za-z _]*$/;
    //The condition checks that the value of the element matches the reg-ex
    if(elem.value.match(alphaExp)){
        // in the case it does, the function returns true.
        return true;
    } else{
        //if it doesnt, an alert message is displayed and the 
        //function returns false.
        alert("please enter text only");
        return false;
    }

}


function check() {
    if (document.getElementById("1").checked==true && document.getElementById('deadline').value.indexOf("true")!=-1) {
        document.getElementById("form").target="_blank";
        document.getElementById("form").action = "startReview.php?version_id="+document.getElementById("version").value+"&conference_id=" +document.getElementById("conf").value;
    //"startReview.php?id="+document.getElementById("paper").value+"&confid=" +document.getElementById("conf").value;
    //window.location = "startReview.php?id="+document.getElementById("paper").value+"&confid=" +document.getElementById("conf").value;
    }  
    else if(document.getElementById("1").checked==true && document.getElementById('deadline').value.indexOf("true")==-1)
    {
        alert('Deadline for reviews is already past');
        document.getElementById('form').action="";
    }
    // directing to the downloadForReview page with the variables needed to download the pdf
    if (document.getElementById("offline").checked && document.getElementById('deadline').value.indexOf("true")!=-1) {
        document.form.action = "DownloadForReview.php?id="+document.getElementById("paper").value+"&confid="+document.getElementById("conf").value+"&verid="+document.getElementById("version").value;
    } 
    else if(document.getElementById("offline").checked && document.getElementById('deadline').value.indexOf("true")==-1)
    {
        alert('Deadline for reviews is already past');
        document.getElementById('form').action="";
    }
}
            
            
$('#go').click (function(){
    if (document.getElementById("offline").checked || document.getElementById("1").checked) {
               
    } 
    else {
        alert ('no button checked');
    }
});


   // Declaring valid date character, minimum year and maximum year
            var dtCh= "/";
            var minYear=1900;
            var maxYear=2100;
            // checking if user input is an integer
            function isInteger(s){
                var i;
                for (i = 0; i < s.length; i++){
                    // Check that current character is number.
                    var c = s.charAt(i);
                    if (((c < "0") || (c > "9"))) return false;
                }
                // All characters are numbers.
                return true;
            }

            function stripCharsInBag(s, bag){
                // passing through all characters in (s) checking if they are lessa than zero
                var i;
                var returnString = "";
                // Search through string's characters one by one.
                for (i = 0; i < s.length; i++){
                    var c = s.charAt(i);
                    if (bag.indexOf(c) == -1) returnString += c;
                }
                return returnString;
            }
            //creating days array for each month depending on no of days per each month
            // looping over the input to give the right number of days per each month
            //(4,6,9,11) = 30 days, (2) = 29 days else no of days = 31
            function DaysArray(n) {
                for (var i = 1; i <= n; i++) {
                    this[i] = 31
                    if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
                    if (i==2) {this[i] = 29}
                }
                return this
            }
            //check the poition of "/", length of day, month, year input
            function isDate(dtStr){
                var daysInMonth = DaysArray(12) // calling function DaysArray for month 12
                var pos1=dtStr.indexOf(dtCh) // checking position of /
                var pos2=dtStr.indexOf(dtCh,pos1+1) // checking position of second /
                var strMonth=dtStr.substring(0,pos1) // the month should be from the start of string till the first /
                var strDay=dtStr.substring(pos1+1,pos2) // day should be after the first / till second /
                var strYear=dtStr.substring(pos2+1) // year after the second / till end of string
                strYr=strYear
                if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
                if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
                // checking the length of each string
                for (var i = 1; i <= 3; i++) { // looping over the input year should be of 4 digits
                    if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
                }
                //parsing each string to int
                month=parseInt(strMonth)
                day=parseInt(strDay)
                year=parseInt(strYr)
                if (pos1==-1 || pos2==-1){ // checking positions of /
                    ChangeText("The date format should be : mm-dd-yyyy")
                    return false
                }
                //checking number of characters per month input and should be between 1 & 12
                if (strMonth.length<1 || month<1 || month>12){
                    ChangeText("Please enter a valid month")
                    return false
                }
                //checking number of characters per day input and should be between 1 & 31
                if (strDay.length<1 || day<1 || day>31 || day > daysInMonth[month]){
                    ChangeText("Please enter a valid day")
                    return false
                }
                //checking number of characters per year input and should be between max and min year
                if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
                    ChangeText("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
                    return false
                }
                //checking position of the (/)
                if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
                    ChangeText("Please enter a valid date")
                    return false
                }
                return true
            }
            // calling previus functions on user input
            function ValidateForm(){ // calling function isDate on the "form" to return true or false
                var dt=document.form.txtDate
                if (isDate(dt.value)==false){
                    dt.focus()
                    return false
                }
                return true
            }
            //change the taxt in the label to view errors or success massages
            function ChangeTextlabel1(text) {
                document.getElementById("label1").innerHTML= text;
            }
            function ChangeTextlabel2(text) {
                document.getElementById("label2").innerHTML= text;
            }
            
              //this part of the code is for hiding and showing the messages table
            $(document).ready(function(){
                $(".flip").click(function(){
                    $(".panel").slideToggle("slow");
                });
            });